﻿using Business.Service;
using Model;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAppWeb.Controllers
{
    public class ReceiveMaterialController : BaseDataController
    {
        //
        // GET: /DeliveryBox/
        public ActionResult ReceiveMaterial()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }



        ///<summary>
        ///查询预估订单信息
        ///</summary>
        ///<returns></returns>
        public JsonResult InitJsonResult(string startDate, string endDate, string saleorderflag, string selType)
        {
            string where = "  where 1=1 ";
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;


            var retData = new object();
            BEstimateProduction db = new BEstimateProduction();
            retData = db.GetZXKCReceiveMaterial(page, rows, out total, selType).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", retData);
            return Json(json);
        }


        /// <summary>
        /// 导出预估生产单、领料单、加工单信息
        /// </summary>
        /// <param name="dbbcBatchNo"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public FileResult ExporterExcel(string startDate, string endDate, string status, string selType)
        {

            #region 获取数据源
            string where = "  where 1=1 ";

            BEstimateProduction db = new BEstimateProduction();

            List<ReceiveMaterial> listRM = null;
            List<ReceiveMaterial> listSumWoc = null;
            listRM = db.GetZXKCReceiveMaterialByEx(selType).ToList();//获取领料单信息

            listSumWoc = db.GetGoodsSumWocByEx().ToList();//获取订单所需商品总数

            #endregion

            #region 导出Excel

            //创建Excel文件的对象
            HSSFWorkbook book = new HSSFWorkbook();
            //添加一个sheet
            ISheet addr = book.CreateSheet("商品总数");
            ISheet addrEstimate = book.CreateSheet("领料单");



            ICellStyle style = book.CreateCellStyle();
            style.BorderBottom = BorderStyle.DashDot;
            style.BorderLeft = BorderStyle.DashDot;
            style.BorderRight = BorderStyle.DashDot;
            style.BorderTop = BorderStyle.DashDot;


            #region 商品总数
            for (int j = 0; j < (listSumWoc.Count + 1); j++)
            {
                addr.CreateRow(j);
                addr.GetRow(j).Height = 400;

                if (j == 0)
                {
                    addr.GetRow(0).CreateCell(0).SetCellValue("货号");
                    addr.GetRow(0).CreateCell(1).SetCellValue("品名");
                    addr.GetRow(0).CreateCell(2).SetCellValue("所需数量");
                    addr.GetRow(0).CreateCell(3).SetCellValue("配送日期");
                }
                else
                {


                    addr.GetRow(j).CreateCell(0).SetCellValue(listSumWoc[j - 1].GOODSCODE);
                    addr.GetRow(j).CreateCell(1).SetCellValue(listSumWoc[j - 1].GOODSNAME);
                    addr.GetRow(j).CreateCell(2).SetCellValue(Convert.ToInt32(listSumWoc[j - 1].SUMQTY));
                    addr.GetRow(j).CreateCell(3).SetCellValue(Convert.ToString(listSumWoc[j - 1].RATIONDATE));
                }

            }
            #endregion

            #region 领料单
            Hashtable hshTableEstimate = new Hashtable(); //  创建哈希表 
            for (int i = 0; i < listRM.Count + 1; i++)
            {
                addrEstimate.CreateRow(i);
                addrEstimate.GetRow(i).Height = 400;

                if (i == 0)
                {
                    addrEstimate.GetRow(0).CreateCell(0).SetCellValue("库位");
                    addrEstimate.GetRow(0).CreateCell(1).SetCellValue("货号");
                    addrEstimate.GetRow(0).CreateCell(2).SetCellValue("品名");
                    addrEstimate.GetRow(0).CreateCell(3).SetCellValue("原材料规格");
                    addrEstimate.GetRow(0).CreateCell(4).SetCellValue("库存单位");
                    addrEstimate.GetRow(0).CreateCell(5).SetCellValue("库存单位领料量");
                    addrEstimate.GetRow(0).CreateCell(6).SetCellValue("采购单位");
                    addrEstimate.GetRow(0).CreateCell(7).SetCellValue("采购单位领料量");

                }
                else if (i > 0)
                {

                    addrEstimate.GetRow(i).CreateCell(0).SetCellValue(listRM[i - 1].LOOKUP);
                    addrEstimate.GetRow(i).CreateCell(1).SetCellValue(listRM[i - 1].GOODSCODE);
                    addrEstimate.GetRow(i).CreateCell(2).SetCellValue(listRM[i - 1].GOODSNAME);
                    addrEstimate.GetRow(i).CreateCell(3).SetCellValue(listRM[i - 1].GOODSSPEC);
                    addrEstimate.GetRow(i).CreateCell(4).SetCellValue(listRM[i - 1].UNITNAME);
                    addrEstimate.GetRow(i).CreateCell(5).SetCellValue(Convert.ToDouble(listRM[i - 1].SUMQTY));

                    addrEstimate.GetRow(i).CreateCell(6).SetCellValue(listRM[i - 1].PURUNITNAME);
                    addrEstimate.GetRow(i).CreateCell(7).SetCellValue(Convert.ToDouble(listRM[i - 1].PURUNITQTY));

                }
            }
            #endregion


            #endregion
            string excelName = "在线库存领料单";
            if (selType == "1")
            {
                excelName = "在线库存当日达领料单";
            }
            else if (selType == "2")
            {
                excelName = "在线库存次日达领料单";
            }
            // 写入到客户端 
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            return File(ms, "application/vnd.ms-excel", NewTimeStamp(excelName));
        }


        ///<summary>
        ///查询前置仓仓库信息
        ///</summary>
        ///<returns></returns>
        public string GetOrderInfo(string startDate, string endDate, string status, string selType)
        {
            string sjson = string.Empty;
            var retData = new object();
            BEstimateProduction db = new BEstimateProduction();
            AppUserList model = (AppUserList)Session["UserInfo"];
            bool b = false;
            List<PsOrder> listRM = db.GetPSOrderInfo(startDate, endDate, status, selType).ToList();
            if (listRM != null && listRM.Count > 0)
            {
                db.DeleteTempOrder();

                b = db.AddTempOrder(listRM);
                if (b)
                {
                    sjson = "{success:true}";
                }
                else
                {
                    sjson = "{success:false,msg:'同步数据失败请联系技术！'}";
                }


            }
            else
            {
                sjson = "{success:false,msg:'配送系统没有数据！'}";
            }
            return sjson;
        }


    }
}
